//package com.plian.system.service.hr.passport.impl;
//
//import com.alibaba.fastjson.JSONObject;
//import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
//import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
//import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
//import com.plian.Tools.*;
//import com.plian.Tools.pagedata.PageData;
//import com.plian.system.constant.sys.FormStatusConstant;
//import com.plian.system.constant.sys.FormTypeConstant;
//import com.plian.system.entity.hr.passport.PassportManagement;
//import com.plian.system.mapper.hr.passport.PassportManagementMapper;
//import com.plian.system.mybatis.base.BaseFlowService;
//import com.plian.system.mybatis.base.BaseServiceImpl;
//import com.plian.system.mybatis.search.SearchEntity;
//import com.plian.system.mybatis.search.SearchParamEntiy;
//import com.plian.system.mybatis.support.Condition;
//import com.plian.system.service.hr.passport.IPassportManagementService;
//import com.plian.system.service.wf.FormWorkflowService;
//import com.plian.system.vo.hr.passport.PassportManagementVO;
//import com.plian.system.wrapper.hr.passport.PassportManagementWrapper;
//import lombok.AllArgsConstructor;
//import org.apache.commons.lang.exception.ExceptionUtils;
//import org.apache.commons.lang3.StringUtils;
//import org.springframework.stereotype.Service;
//import org.springframework.transaction.annotation.Transactional;
//
//import java.util.*;
//
//import static com.plian.system.constant.sys.FormStatusConstant.UNREVIEWED;
//
///**
// *  服务实现类
// *
// * @author gangan
// * @since 2019-12-02
// */
//@Service
//@AllArgsConstructor
//public class PassportManagementServiceImpl extends BaseServiceImpl<PassportManagementMapper, PassportManagement> implements IPassportManagementService, BaseFlowService {
//
//    private FormWorkflowService formWorkflowService;
//
//    private PassportManagementWrapper passportManagementWrapper;
//
//    @Override
//    @Transactional(rollbackFor=RuntimeException.class)
//    public boolean saveOrUpdate(PassportManagement passportManagement) {
//        if(passportManagement.getStatus()==null){
//            passportManagement.setStatus(FormStatusConstant.UNREVIEWED);
//        }
//        return super.saveOrUpdate(passportManagement);
//    }
//
//    @Override
//    public boolean deleteLogic(List<Long> ids) {
//    for (Long id : ids) {
//        PassportManagement passportManagement = getById(id);
//        if (passportManagement == null) {
//            return false;
//        } else if (passportManagement.getStatus() != null &&
//                (passportManagement.getStatus() != FormStatusConstant.STASH && passportManagement.getStatus() != FormStatusConstant.UNREVIEWED)) {
//            return false;
//        }
//        formWorkflowService.deleteProcessInstance(String.valueOf(id));
//    }
//    return super.deleteLogic(ids);
//    }
//
//    @Override
//    public HashMap<String,Object> commit(String id) {
//        handleStatus(Long.valueOf(id), FormStatusConstant.FLOW);
//
//        //将需要提交的流程变量返回，用于工作流侧
//        return new HashMap<String,Object>();
//    }
//
//    @Override
//    public Boolean finish(String id) {
//        return handleStatus(Long.valueOf(id), FormStatusConstant.FINISH);
//    }
//
//    @Override
//    public Boolean reject(String id, int formState, String comment) {
//        return handleStatus(Long.valueOf(id), formState);
//    }
//
//    /**
//     * 统一处理状态机事件方法
//     * @param id 主键
//     * @param updateStatus 更新状态
//     * @return boolean
//     */
//    private Boolean handleStatus(Long id, int updateStatus) {
//        try {
//            PassportManagement passportManagement = getById(id);
//            /**
//             * 设置审批人和审批时间
//             */
//            if (updateStatus == FormStatusConstant.FINISH){
//                passportManagement.setApprovedUser(TokenUtil.getTokenUserId());
//                Date now = DateUtil.now();
//                passportManagement.setApprovedTime(now);
//            }
//            passportManagement.setStatus(updateStatus);
//            updateById(passportManagement);
//            return true;
//        } catch (Exception e) {
//            log.error(ExceptionUtils.getFullStackTrace(e));
//            return false;
//        }
//    }
//
//	@Override
//	public boolean lend(Long id) {
//		PassportManagement passportManagement = getById(id);
//		passportManagement.setDef1("已借出");
//		UpdateWrapper<PassportManagement> updateWrapper = new UpdateWrapper<>();
//		return update(passportManagement,updateWrapper.eq("id",passportManagement.getId()));
//	}
//
//	@Override
//    @Transactional(rollbackFor=RuntimeException.class)
//	public boolean reback(Long id) {
//		PassportManagement passportManagement = getById(id);
//		passportManagement.setDef1("未借出");
//		UpdateWrapper<PassportManagement> updateWrapper = new UpdateWrapper<>();
//		return update(passportManagement,updateWrapper.eq("id",passportManagement.getId()));
//	}
//
//	@Override
//	public java.io.File exportExcel(JSONObject jsonObject) {
//		List<SearchEntity> entityList = Optional.ofNullable(jsonObject.getJSONArray("query"))
//			.map(x -> x.toJavaList(SearchEntity.class)).orElse(new ArrayList<>());
//		QueryWrapper<PassportManagement> qw= SearchParamEntiy.getQueryWrapper(
//			Condition.getQueryWrapper(new PassportManagement()), entityList
//		);
//
//		List<PassportManagement> passportManagements = list(qw);
//		return null;
//		//return ExcelUtils.exportExcelByList(jsonObject, PassportManagementWrapper.build().listVO(passportManagements));
//	}
//
//    @Override
//    public List<PageData> findByIds(List<String> formIds) {
//        LambdaQueryWrapper<PassportManagement> queryWrapper = new LambdaQueryWrapper<>();
//        queryWrapper.in(PassportManagement::getId, Func.toLongList(StringUtils.join(formIds, StringPool.COMMA)));
//        List<PassportManagement> passportManagements = list(queryWrapper);
//        List<PassportManagementVO> passportManagementVOS = passportManagementWrapper.entityToVO(passportManagements);
//        List<PageData> pageDataList = new ArrayList<>();
//        try{
//            if (CollectionUtil.isNotEmpty(passportManagementVOS)){
//                for (PassportManagementVO vo : passportManagementVOS){
//                    PageData pd = new PageData();
//                    pd.put("id", vo.getId());
//                    pd.put("UNITNAME", vo.getCompanyName());
//                    pd.put("APPLICANTNAME", vo.getCreateUserName());
//                    pd.put("applicantId", vo.getCreateUser());
//                    pd.put("applyOrganizationId", vo.getCompanyNameId());
//                    pd.put("formType", FormTypeConstant.HR_PASSPORT_MANAGEMENT_FORM);
//                    pd.put("orgName", vo.getCompanyName());
//                    pageDataList.add(pd);
//                }
//            }
//
//            return pageDataList;
//        } catch (Exception e) {
//            log.error(ExceptionUtils.getFullStackTrace(e));
//        }
//        return pageDataList;
//    }
//
//    @Override
//    public List<Integer> getFormType() {
//        return Arrays.asList(FormTypeConstant.HR_PASSPORT_MANAGEMENT_FORM);
//    }
//
//    @Override
//    public void goBackToStash(String formId) {
//        handleStatus(Long.valueOf(formId), UNREVIEWED);
//    }
//}
